package com.lt.crm.controller;


import com.lt.crm.pojo.SysRight;
import com.lt.crm.pojo.SysRole;
import com.lt.crm.pojo.SysUser;
import com.lt.crm.service.SysRightService;
import com.lt.crm.service.SysRoleService;
import com.lt.crm.service.SysUserService;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;


/**
 * 提供项目的入口指向
 */
@Controller
public class IndexController {

    @Resource
    SysRoleService sysRoleService;
    @Resource
    SysUserService sysUserService;
    @Resource
    SysRightService sysRightService;
    @Resource
    StringRedisTemplate stringRedisTemplate;
    // redis中数据key的前缀
    private String SHIRO_LOGIN_COUNT = "shiro_login_count_";//登陆计数

    /**
     * 跳转至登录
     *
     * @return
     */
    @RequestMapping("/index")
    public String index() {
        return "login";
    }

    /**
     * 跳转至主页
     *
     * @return
     */
    @RequestMapping("/main")
    public String main(HttpSession session) {
        System.out.println(session.getAttribute("loginUser"));
        return "main";
    }

    /**
     * 登录
     *
     * @param usrName
     * @param usrPassword
     * @return
     */
    @RequestMapping("/dologin")
    public String doLogin(Model model,
                          String usrName, String usrPassword,
                          HttpSession session) {
        try {
            // 封装前先进行加密
            //usrPassword = sysUserService.encryptPassword(usrPassword);
            System.out.println(usrPassword);
            UsernamePasswordToken token = new UsernamePasswordToken(usrName, usrPassword);
            Subject subject = SecurityUtils.getSubject();
            token.setRememberMe(true);
            subject.login(token);//认证、登录
            // 认证成功，清空登录计数
            stringRedisTemplate.delete(SHIRO_LOGIN_COUNT+usrName);
            //认证成功
            SysUser user = (SysUser) subject.getPrincipal();
            System.out.println(user);
            // 获取权限
            SysRole role =sysRoleService.getById(user.getUsrRoleId());
            user.setRole(role);
            List<SysRight> rights = sysRightService.findRightsByRole(role);
            role.setRights(rights);
            session.setAttribute("loginUser", user);
            return "redirect:main";
        } catch (UnknownAccountException | IncorrectCredentialsException e) {
            model.addAttribute("message", "登录失败，用户名或密码错误");
            return "login";
        } catch (LockedAccountException e) {
            model.addAttribute("message", "用户被禁用，登录失败！");
            return "login";
        } catch (AuthenticationException e) {
            model.addAttribute("message", "认证失败，登录异常！");
            return "login";
        }
    }

    /**
     * 退出登录
     *
     * @param session
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("loginUser");
        //Shiro注销
        SecurityUtils.getSubject().logout();
        //重定向、删除Cookie
        return "redirect:/index";
    }

    @RequestMapping("/403")
    public String noPower(){
        return "403";
    }
}
